package com.yusi.controller;


import com.yusi.entity.Admin;
import com.yusi.entity.dto.RespBean;
import com.yusi.entity.vm.AdminLogin;
import com.yusi.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@RestController
@Api(tags = "LoginController")
public class LoginController {
    @Autowired
    private IAdminService iAdminService;

    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public RespBean login(@RequestBody AdminLogin adminLogin, HttpServletRequest request){
        return  iAdminService.Login(adminLogin.getUsername(),adminLogin.getPassword(),adminLogin.getCode(),request);
    }
    @ApiOperation("获取用户信息")
    @GetMapping("admin/info")
    public Admin getAdminInfo(Principal principal){
        if (principal==null){//如果为null则说明登录有问题
            return null;
        }
        String username = principal.getName();
       Admin admin=iAdminService.getAdminByName(username);
       admin.setPassword(null);

       admin.setRoles(iAdminService.getRoles(admin.getId()));
       return admin;
    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public RespBean logout(){
        return RespBean.success("退出登录成功");
    }


}
